package com.expert.miniapp.controller;

import com.expert.miniapp.dao.ResearchExpert;
import com.expert.miniapp.dao.ResearchExpertMapper;
import com.expert.miniapp.dto.ResultDTO;
import com.expert.miniapp.service.ResearchExpertService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 科研专家控制器
 * 提供专家信息的REST API接口
 */
@RestController
@RequestMapping("/api/experts")
@CrossOrigin(origins = "*")
public class ResearchExpertController {

    @Autowired
    private ResearchExpertService researchExpertService;

    @Autowired
    private ResearchExpertMapper researchExpertMapper;

    /**
     * 根据ID获取专家详细信息
     * @param id 专家ID
     * @return 专家详细信息
     */
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public ResultDTO<ResearchExpert> getExpertDetails(@PathVariable String id) {
        try {
            ResearchExpert expert = researchExpertService.getExpertById(id);
            if (expert != null) {
                return ResultDTO.success(expert);
            } else {
                return ResultDTO.error("专家不存在");
            }
        } catch (Exception e) {
            return ResultDTO.error("查询专家信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取专家列表（支持分页、分类筛选、权重推荐、模糊搜索）
     * @param pageNum 页码（可选）
     * @param pageSize 每页数量（可选）
     * @param category 分类（可选）
     * @param sortBy 排序方式，weight表示按权重推荐（可选）
     * @param keyword 搜索关键词（可选）
     * @return 专家列表
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public ResultDTO<PageInfo<ResearchExpert>> getExpertList(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String keyword) {
        try {
            PageInfo<ResearchExpert> pageInfo;

            if (keyword != null && !keyword.trim().isEmpty()) {
                pageInfo = researchExpertService.searchExperts(keyword, pageNum, pageSize);
            } else if ("weight".equalsIgnoreCase(sortBy)) {
                pageInfo = researchExpertService.getRecommendedExperts(pageNum, pageSize);
            } else if (category != null && !category.trim().isEmpty()) {
                pageInfo = new PageInfo<>(researchExpertService.getExpertsByCategory(category));
            } else {
                pageInfo = researchExpertService.getAllExperts(pageNum, pageSize);
            }

            return ResultDTO.success(pageInfo);
        } catch (Exception e) {
            return ResultDTO.error("查询专家列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据分类统计专家数量
     * @param category 分类名称（如："人工智能"）
     * @return 包含该分类的专家数量
     */
    @GetMapping(value = "/categories/statistics", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public ResultDTO<Map<String, Integer>> getCountByCategory(@RequestParam String category) {
        try {
            Integer count = researchExpertMapper.countByCategory(category);
            Map<String, Integer> result = new HashMap<>();
            result.put("count", count != null ? count : 0);
            return ResultDTO.success(result);
        } catch (Exception e) {
            return ResultDTO.error("统计专家数量失败: " + e.getMessage());
        }
    }
}